<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 13-9-12
 * Time: 上午12:43
 * To change this template use File | Settings | File Templates.
 */

class users_model extends CI_Model {

    function lists($page = 1, $per_page = 20) {
        $this->db->start_cache();
        $this->db->select('id', 'name', 'password', 'account', 'phone');
        $this->db->from('users');
        $this->db->order_by('name', 'asc');
        $this->db->stop_cache();
        $total = $this->db->count_all_results();
        if ($per_page > 0) {
            $this->db->limit($per_page, ($page - 1) * $per_page);
        }
        $query = $this->db->get();
        $rows = $query->result_array();
        return array('total' => $total, 'rows' => $rows);
    }

    function get($id) {
        $this->db->select('id', 'name', 'password', 'account', 'phone');
        $this->db->from('users');
        $this->db->where('id', $id);
        $query = $this->db->get();
        $row = $query->result_array();
        return $row;
    }

    function add($data) {
        $this->db->insert('users', $data);
    }

    function update($id, $data) {
        $this->db->where('id', $id);
        $this->db->update('users', $data);
    }

    function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete('users');
    }
}